/**
 * 
 */
package edu.fjnu.fundsys.service;

import java.util.List;

import edu.fjnu.fundsys.dao.HotelDao;
import edu.fjnu.fundsys.dao.HotelDaoJDBCImpl;
import edu.fjnu.fundsys.dao.RoomDao;
import edu.fjnu.fundsys.dao.RoomDaoJDBCImpl;
import edu.fjnu.fundsys.domain.Hotel;
import edu.fjnu.fundsys.domain.Room;
import edu.fjnu.fundsys.utils.Page;

/**
 * @author Administrator
 *
 */
public class RoomServiceImpl implements RoomService {

	/* (non-Javadoc)
	 * @see edu.fjnu.hotelsys.service.RoomService#createRoom(edu.fjnu.hotelsys.domain.Room)
	 */
	public void createRoom(Room room) {
		
		RoomDao roomDao=new RoomDaoJDBCImpl();
		roomDao.add(room);
		
		HotelDao hotelDao=new HotelDaoJDBCImpl();
		Hotel hotel=hotelDao.getHotelByNo(room.getHotel().getHotelNo());
		hotel.setHotelRoomCount(hotel.getHotelRoomCount()+1);
		hotelDao.update(hotel);

	}

	@Override
	public Room getRoomById(Integer roomId) {
		RoomDao roomDao=new RoomDaoJDBCImpl();
		return roomDao.getRoomById(roomId);
	}

	@Override
	public List<Room> loadAll() {
		RoomDao roomDao=new RoomDaoJDBCImpl();
		return roomDao.loadAll();
	}

	@Override
	public void removeRoom(Integer roomId) {
		
		RoomDao roomDao=new RoomDaoJDBCImpl();
		Hotel hotel=roomDao.getRoomById(roomId).getHotel();
		roomDao.delete(roomId);
		
		HotelDao hotelDao=new HotelDaoJDBCImpl();
		hotel.setHotelRoomCount(hotel.getHotelRoomCount()-1);
		hotelDao.update(hotel);
		
	}

	@Override
	public void updateRoom(Room room) {
		
		RoomDao roomDao=new RoomDaoJDBCImpl();
		Hotel oldHotel=roomDao.getRoomById(room.getRoomId()).getHotel();
		roomDao.update(room);
		
		if(!room.getHotel().getHotelNo().equals(oldHotel.getHotelNo())){
			
			HotelDao hotelDao=new HotelDaoJDBCImpl();
			oldHotel.setHotelRoomCount(oldHotel.getHotelRoomCount()-1);
			hotelDao.update(oldHotel);
			
			Hotel newHotel=hotelDao.getHotelByNo(room.getHotel().getHotelNo());
			newHotel.setHotelRoomCount(newHotel.getHotelRoomCount()+1);
			hotelDao.update(newHotel);
		}
			
		
	}

	@Override
	public List<Room> loadRooms(RoomQueryHelper helper) {
		RoomDao roomDao=new RoomDaoJDBCImpl();
		return roomDao.loadRooms(helper);
	}

	@Override
	public Page loadPagedRooms(Page page, RoomQueryHelper helper) {
		RoomDao roomDao=new RoomDaoJDBCImpl();
		page.setTotalRecNum(roomDao.cntRooms(helper));
		page.setPageContent(roomDao.getScopedRooms(helper, page.getStartIndex(), page.getEndIndex()));
		return page;
	}

}
